{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4511bdb2",
   "metadata": {},
   "source": [
    "### 1.引入函数\n",
    "> 1.函数引入两个关键字：def和return  \n",
    "> 2.函数可以接受参数数据（即函数的输入）  \n",
    "> 3.函数包含代码，（通常）还有文档  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fcba0d9",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cbe7f39",
   "metadata": {},
   "source": [
    "### 字符串引号字符\n",
    "* 加三重引号的字符串成为docstring,用来作为文档\n",
    "* 用单引号(')或双引号(\")包围的字符串不能跨多行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff2ab8d9",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f71e178",
   "metadata": {},
   "source": [
    "### 元音字母程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "03784fe1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a wordeufwruivbe\n",
      "i\n",
      "e\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "#第三章vowels.py程序：给定单词，便输出该词包含的元音\n",
    "\n",
    "vowels = set('aeiou')\n",
    "word = input('provide a word')   # 采用PEP8的建议，一致用单引号字符包围字符串\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "22fca05f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理后：\n",
    "def search4vowels(word):      # 将参数名放在小括号内\n",
    "    \"\"\"display any vowels found in a suppied word\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found =vowels.intersection(set(word))\n",
    "    for vowels in found:\n",
    "        print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7aef9c41",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6863e9a6",
   "metadata": {},
   "source": [
    "### 函数返回一个结果\n",
    "* python 提供一个内置函数，名为bool，提供某个值时，它会告诉你这个值计算为True 还是 false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6326312d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# false的例子\n",
    "bool(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "186660da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool([])   # 空串、空列表和空字典都计算为false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4dd50c21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ture的例子\n",
    "bool(1)       # 不为0、非空的数都是true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "087f8310",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eaee86df",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用返回一个值改进代码 --> 根据是否找到元音来返回true false \n",
    "# 如果什么都没找到，函数返回false;否则，返回true\n",
    "\n",
    "def search4vowels(word):  \n",
    "    \"\"\"display any vowels found in a suppied word\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found =vowels.intersection(set(word))\n",
    "    return bool(found)     # 调用bool函数   found --> 传入一个数据结构名，其中包含搜索元音的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1d9535e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('hitch-hiker')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74eb06df",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "69771752",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 使用返回多个值改进代码\n",
    "def search4vowels(word):      \n",
    "    \"\"\"display any vowels found in a suppied word\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(word))    # 要让函数返回多个值，而不是一个布尔值，且删除不必要的found变量,可以直接返回intersection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d48a8e2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels('hitch-hiker')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab3d2fe2",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aec19dfa",
   "metadata": {},
   "source": [
    "* set() 为空字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdbb0073",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b5a3545",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用注释改进文档\n",
    "\n",
    "def search4vowels(word:str) -> set:       # str 表示希望’word‘参数是一个字符串； set 表示函数想其调用者返回一个集合\n",
    "    \"\"\"display any vowels found in a suppied word\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(word)) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ad03a80",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b040341a",
   "metadata": {},
   "source": [
    "### 建立一个通用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b966e84",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(phrase:str) -> set:      \n",
    "    \"\"\"display any vowels found in a suppied word\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(phrase))  # 将word变量名改为更合适的phrase"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b95caba",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ba168ae",
   "metadata": {},
   "source": [
    "* 允许用户指定要搜索的字母集合，而不总是使用5个元音"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f515ab4e",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efabc27e",
   "metadata": {},
   "source": [
    "### 创建另一个函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "948cc206",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set(letters) --> 从'letters'创建一个集合对象\n",
    "# set(phrase) --> 从'letters' 创建的集合对象与从'phrase'创建的集合对象完成一个交集操作\n",
    "\n",
    "def sesrch4letters(phrase:str, letters: str) ->set:       # 指定参数列表\n",
    "    \"\"\"Returns a set of 'letters' found in 'phrase'.\"\"\"\n",
    "    return set(letters).intersection(set(phrase))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "576d5c76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x', 'y'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sesrch4letters('galaxy','xyz')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28e7feb7",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "872b0fb5",
   "metadata": {},
   "source": [
    "### 位置赋值与关键字赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72bdce50",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果提供一个参数，letters参数酒默认为一个元音字符串\n",
    "def sesrch4letters(phrase:str, letters: str='aeiou') ->set:\n",
    "    \"\"\"Returns the set of 'letters' found in 'phrase'.\"\"\"\n",
    "    return set(letters).intersection(set(phrase))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb589d4",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e721fe6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# \"vsearch.py模块中的函数，包含两个函数\n",
    "def search4vowels(phrase:str) -> set:\n",
    "    \"\"\"Returns the set of vowels found in 'phrase'.\"\"\"\n",
    "    return set('aeiou').intersection(set(phrase))\n",
    "\n",
    "def sesrch4letters(phrase:str, letters: str='aeiou') ->set:\n",
    "    \"\"\"Returns the set of 'letters' found in 'phrase'.\"\"\"\n",
    "    return set(letters).intersection(set(phrase))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9468711d",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c8a26c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# \"setup.py\"文件，允许我们将模块变成一个可安装的发布包 \n",
    "from setuptools import setup   # 从'setuptools'模块导入'setup'模块\n",
    "setup(\n",
    "    name='vsearch',            # name 参数指定发布包。常见的做法是模块命名发布包\n",
    "    version='1.0',\n",
    "    description='The Head First Python Search Tools',\n",
    "    author='HF Python 2e',\n",
    "    author_email='hfpy2e@gmail.com',\n",
    "    url='headfirstlabs.com',\n",
    "    py_modules=['vsearch'],)     # 这里包含在这个包中的所有'.py'文件的列表。对于这个例子，只有一个文件：'vsearch'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69c44d3a",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad9e08a1",
   "metadata": {},
   "source": [
    "### 在windows 上创建一个发布文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "710b4670",
   "metadata": {},
   "outputs": [],
   "source": [
    "# c:\\user\\Head first\\mymodules> py -3 setup.py sdist\n",
    "\n",
    "# 指的是在windowd 上运行python3 ,执行'setup.py'中的代码，并传递'sdist'作为参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c5ab729",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e98e694",
   "metadata": {},
   "source": [
    "### 按值模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "347bc1b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def double(arg):\n",
    "    print('Before: ', arg)\n",
    "    arg = arg * 2          # 函数将传入的值加倍\n",
    "    print('After: ', arg)\n",
    "    \n",
    "def change(arg: list):\n",
    "    print('Before: ', arg)\n",
    "    arg.append('More data')    # 函数为传入的列表追加一个字符串\n",
    "    print('After: ', arg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
